/*
 * net_file.c
 *
 *  Created on: 2023年8月30日
 *      Author: HXF
 */

#include "MacCrypto.h"
#include "net_addressing.h"
#include "net_packet.h"
#include "net_file.h"
#include "net_msgseq.h"
#include "RBFCommon.h"
#include "sl_malloc.h"
#include "app_log.h"
#include "device.h"
#include "net_rolling_code.h"
#include "app_file_packet_api.h"
#include "app_filetransport_api.h"
#include "app_broadcast_retry.h"
#ifdef  HUB_DEVICE
#include "app_hub_api_def.h"
#endif
#define FILE_LINK_LENGTH 255

#define FILE_MALLOC sl_malloc
#define FILE_FREE sl_free

#ifdef  HUB_DEVICE

static uint8_t *ptr_packet_tx = NULL;
static char check_flag = 0;
static char now_retry = 0;
static char max_retry = 0;
#endif

void fill_file_hdr(uint8_t destID, uint8_t srcId, uint8_t cmd, uint8_t * packet)
{
#ifdef  HUB_DEVICE
  RBFMacHeader_t hdr;
  uint8_t ver = 0;
  ver = HUB_VER;
  // heander
  hdr.rollungCode = get_device_itself_rolling_code();
  hdr.frameCtl = (0 << FRAMECTL_TPMODE_POS) | (1 << FRAMECTL_BURSTMODE_POS)
                  | (ver & FRAMECTL_VERSION_MASK);
  hdr.destID = destID; // 目的地址
  hdr.panId = readThisSystemId();//修改读取systemID,作废readThisPanId();
  hdr.srcId = srcId;
  hdr.cmd = cmd;
  RBF_packet_build_header(hdr, packet);
#else
  (void)destID;
  (void)srcId;
  (void)cmd;
  (void)packet;
#endif
}

int fill_file_transport_hdr(void)
{
#ifdef  HUB_DEVICE
  fill_file_hdr(0xff, 0x00, 0x4A, ptr_packet_tx);
  return 0;
#else
  return 0;
#endif
}

int fill_file_check_hdr(void)
{
#ifdef  HUB_DEVICE
  if(!ptr_packet_tx){
      ptr_packet_tx = FILE_MALLOC(FILE_LINK_LENGTH);
  }
  if(ptr_packet_tx){
      memset(ptr_packet_tx, 0, FILE_LINK_LENGTH);
      fill_file_hdr(0xff, 0x00, 0x4B, ptr_packet_tx);
      return 0;
  }
  return 1;
#else
  return 0;
#endif
}

int fill_file_transport_pkt(void)
{
#ifdef  HUB_DEVICE
  if(!ptr_packet_tx){
      ptr_packet_tx = FILE_MALLOC(FILE_LINK_LENGTH);
  }
  if(ptr_packet_tx){
      memset(ptr_packet_tx, 0, FILE_LINK_LENGTH);
  }else{
      return 1;
  }

  int payload_len = 0;
  production_page_filetransport((unsigned char *)(&ptr_packet_tx[16]), &payload_len);
  if(!payload_len){
      return 2;
  }
  fill_file_transport_hdr();
  ptr_packet_tx[0] = RBF_HEADER_LENGTH_MAX + payload_len + RBF_TAIL_LENGTH_MAX;
  if(file_log_enable){
      DS_app_log_error("file(%d) is:  pageId[%d]\r\n",
                   ptr_packet_tx[0],((ptr_packet_tx[16] << 8)+ptr_packet_tx[17]));
  }
  uint8_t ret = mac_payload_crypto(ptr_packet_tx);
  if (SECURE_ELEMENT_SUCCESS != ret){
      if(file_log_enable){
          DS_app_log_error("crypto ret is %d\r\n", ret);
      }
      return 3;
  }
#endif
  return 0;
}

void fill_file_check_pkt(bool is_retry)
{
#ifdef  HUB_DEVICE
  if(file_log_enable){
      DS_app_log_error("check retry %d\r\n",  is_retry);
  }
  if(fill_file_check_hdr()){
      return;
  }
  int payload_len = 0;
  if(production_check_request((unsigned char *)(&ptr_packet_tx[16]), &payload_len) == -1){
      //TODO 触发异常，退出OTA evt
  }
  if(!payload_len){
      return;
  }

  if(!is_retry){
      max_retry = ptr_packet_tx[19];
  }
  ptr_packet_tx[19] = now_retry;
  ptr_packet_tx[0] = RBF_HEADER_LENGTH_MAX + payload_len + RBF_TAIL_LENGTH_MAX;
  if(file_log_enable){
      DS_app_log_error("check(%d) is:",  ptr_packet_tx[0]);
      DS_app_log_hexdump_error("", &ptr_packet_tx[1], ptr_packet_tx[0]);
  }
  uint8_t ret = mac_payload_crypto(ptr_packet_tx);
  if (SECURE_ELEMENT_SUCCESS != ret){
      if(file_log_enable){
          DS_app_log_error("crypto ret is %d\r\n", ret);
      }
  }
  check_flag = 0;
  start_timer_to_trigger_broadcast_retry(NULL, 0,
                                         NULL, 0,
                                         &now_retry, max_retry,
                                         &check_flag, 1);
#else
  (void)is_retry;
#endif
}

void mac_txStatecallbackFileDataTx (uint8_t** ppPacket,MAC_TX_STATE_E currState,void* pData)
{
#ifdef HUB_DEVICE
  //RETRY_CTRL_ST *pRetryCtrl = (RETRY_CTRL_ST *)pData;
  (void)pData;
  FILE_TRANSPORT_E step;
  if(currState == EN_MAC_TX_STATE_IDLE_PACKET_ASSEMBLE){
#if(1)
      step = get_file_transport_step();
      //APP_PRINTF("F-I %d\r\n",step);
      if(step == PRODUCTION_TRANSPORT_FILE){
          if(check_ota_suspend_custom()){
              check_flag = 1;
              now_retry = 0;
              update_ota_rfm_check_state(0xff,0,NULL,0);
              fill_file_check_pkt(false);
              creat_ota_tx_file_check_event();
              return;
          }else{
              if(fill_file_transport_pkt()){
                  if(ptr_packet_tx){
                      FILE_FREE(ptr_packet_tx);
                      ptr_packet_tx = NULL;
                  }
                  return;
              }
          }
      }else if(step == PRODUCTION_TRANSPORT_CHECK){
          if(check_flag){
              if(now_retry == 0){
                  fill_file_check_pkt(false);
              }else{
                  fill_file_check_pkt(true);
              }
          }
      }else{
          return;
      }
#else
      //fill_file_transport_pkt();
      //fill_file_check_pkt(false);
#endif
      if(ptr_packet_tx){
          *ppPacket = ptr_packet_tx;
      }else{
          return;
      }
  }else if(currState == EN_MAC_TX_STATE_SUCCED){
      net_msgseq_update();
      if(ptr_packet_tx){
          FILE_FREE(ptr_packet_tx);
          ptr_packet_tx = NULL;
      }
      if(file_log_enable){
          APP_PRINTF("F-S\r\n");
      }
  }else if(currState == EN_MAC_TX_STATE_FAILED){
      net_msgseq_update();
      if(ptr_packet_tx){
          FILE_FREE(ptr_packet_tx);
          ptr_packet_tx = NULL;
      }
      if(file_log_enable){
          APP_PRINTF("F-F\r\n");
      }
  }
#else
  (void)ppPacket;
  (void)currState;
  (void)pData;
#endif
}
